package com.bdth.controller;

import com.bdth.model.*;
import com.bdth.service.IDeviceService;
import com.bdth.service.IRealDataService;
import com.bdth.service.ITerminalCommandService;
import com.bdth.utils.HtmlUtil;
import com.bdth.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * @author: 徐斌
 * @Date: 2018-04-13 14:26
 */
@Controller
@RequestMapping("/terminalCommand")
public class TerminalCommandController extends BaseController {

	@Autowired
	private ITerminalCommandService terminalCommandService;

	@Autowired
	private IDeviceService deviceService;

	@Autowired
	private IRealDataService realDataService;

	@RequestMapping("/selectPage")
	public ModelAndView selectPage(String page, int dvcId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> context = getRootMap();
		context.put("dvcId", dvcId);
		return forword("/terminalCommand/" + page, context);
	}

	@RequestMapping("/callTerminal")
	public void callTerminal(int dvcId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> context = getRootMap();
		try {
			//获取设备号
			Device device = deviceService.queryById(dvcId);
			if (device == null) {
				sendFailureMessage(response, "没有找到对应的设备!");
				return;
			} else {
				GPSRealData rd = realDataService.get("realData_" + device.getSimNo());
				if (rd != null) {
					if (rd.getOnline() != null && rd.getOnline() == 3) {
						User user = SessionUtils.getUser(request);
						TerminalCommandNew tc = new TerminalCommandNew();
						tc.setCmdType(33281);
						tc.setPlateNo("");
						tc.setCmd("");
						tc.setCmdData("");
						tc.setUserId(user.getLoginName());
						tc.setOwner(TerminalCommandNew.FROM_PLATFORM);
						tc.setRemark("");
						if (device != null) {
							tc.setSimNo(device.getSimNo());
						} else {
							tc.setSimNo("");
						}

						terminalCommandService.sendCallTerminal(tc);

						context.put(SUCCESS, true);


						//context.put("dvcId", tc.getTerminalCommandID());
						HtmlUtil.writerJson(response, context);
					} else {
						sendFailureMessage(response, "设备不在线,无法点名定位!");
					}

				}


			}
		} catch (Exception ex) {
			sendFailureMessage(response, ex.getMessage());
		}
	}

	/**
	 * 发送短报文命令
	 */
	@RequestMapping("/sendShortMessage")
	public void sendShortMessage(int dvcId, String cmdContent, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> context = getRootMap();
		try {
			BDMessage bd = new BDMessage();
			bd.setMessage(cmdContent);
			User user = SessionUtils.getUser(request);
			//bd.setSendSim("platform");
			bd.setSendSim(user.getUserName());
			bd.setSendDepid("0");
			bd.setSendDepname("platform");
			bd.setIsAnnouncement(0);
			//获取设备号
			Device device = deviceService.queryById(dvcId);
			if (device != null) {
				bd.setRecSim(device.getSimNo());
				bd.setRecDepid("" + device.getDepId());
				bd.setRecDepname(device.getDepName());
			} else {
				bd.setRecSim("");
				bd.setRecDepid("");
				bd.setRecDepname("");
			}
		/*	tc.setState(TerminalCommand.STATUS_NEW);
			tc.setCreateDate(new Date());
			SendCommand(tc, dvcId, request);*/
			terminalCommandService.sendBDMessage(bd);
			context.put(SUCCESS, true);

		/*	//获取设备号
			Device device = deviceService.queryById(dvcId);
			if (device != null) {
				context.put("simNo", device.getSimNo());
			} else {
				context.put("simNo", "");
			}

			context.put("dvcId", tc.getTerminalCommandID());*/
			HtmlUtil.writerJson(response, context);

		} catch (Exception ex) {
			sendFailureMessage(response, ex.getMessage());
		}
	}

	@RequestMapping("/getCommandResult")
	public void getCommandResult(int commandId, HttpServletResponse response) throws Exception {
		Map<String, Object> context = getRootMap();
		TerminalCommand pc = getTerminalCommand(commandId);
		String value = TerminalCommand.TCommandStatus.get(pc.getState());
		String msg = value != null ? value : "";
		if (TerminalCommand.STATUS_NEW.equals(pc.getState()) == false
				&& TerminalCommand.STATUS_PROCESSING.equals(pc.getState()) == false) {
			context.put(SUCCESS, true);
			context.put("msg", msg);
			HtmlUtil.writerJson(response, context);
		}
		context.put(SUCCESS, false);
		context.put("msg", msg);
		HtmlUtil.writerJson(response, context);
	}


	protected void SendCommand(TerminalCommand tc, int dvcId, HttpServletRequest request) throws Exception {
		Device d = deviceService.queryById(dvcId);
		if (d != null) {
			tc.setSimNo(d.getSimNo());
		}

		User user = SessionUtils.getUser(request);
		if (user != null) {
			tc.setUserID(user.getId());
		}
		//terminalCommandService.add(tc);
		terminalCommandService.sendTerminalCommand(tc);
	}


	public TerminalCommand getTerminalCommand(int commandId) throws Exception {
		return terminalCommandService.queryById(commandId);
	}


	/**
	 * 发送短报文命令
	 */
	@RequestMapping("/sendShortMessageBatch")
	public void sendShortMessageBatch(String deviceIds, String cmdContent, int isAnnouncement, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> context = getRootMap();
		String msgSuccess = "";
		String msgFail = "";
		String[] dvcIds = deviceIds.split(",");
		User user = SessionUtils.getUser(request);
		for (String idStr : dvcIds) {
			BDMessage bd = new BDMessage();
			bd.setMessage(cmdContent);
			//bd.setSendSim("platform");
			bd.setSendSim(user.getUserName());
			bd.setSendDepid("0");
			bd.setSendDepname("platform");
			bd.setIsAnnouncement(isAnnouncement);
			//获取设备号
			Device device = deviceService.queryById(Integer.valueOf(idStr));
			if (device != null) {
				bd.setRecSim(device.getSimNo());
				bd.setRecDepid("" + device.getDepId());
				bd.setRecDepname(device.getDepName());
			} else {
				bd.setRecSim("");
				bd.setRecDepid("");
				bd.setRecDepname("");
			}
			try {
				terminalCommandService.sendBDMessage(bd);
				msgSuccess = device.getEquipName() + ":发送成功" + "," + msgSuccess;
			} catch (Exception ex) {
				msgFail = device.getEquipName() + ":发送失败:原因:" + ex.getMessage() + "," + msgFail;
				continue;
			}
		}
		context.put("msgSuccess", msgSuccess);
		context.put("msgFail", msgFail);
		HtmlUtil.writerJson(response, context);

	}
}
